package com.edu.study.base.wordrelation.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.edu.study.base.wordBook.service.IWordBookService;
import com.edu.study.base.wordrelation.domain.WordRelation;
import com.edu.study.base.wordrelation.service.IWordRelationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单词归属关系Controller
 *
 * @author zw
 * @date 2022-04-12
 */
@RestController
@RequestMapping("/study/wordrelation")
public class WordRelationController extends BaseController {
    @Autowired
    private IWordRelationService wordRelationService;

    /**
     * 查询单词归属关系列表
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:list')")
    @GetMapping("/list")
    public TableDataInfo list(WordRelation wordRelation) {
        startPage();
        List<WordRelation> list = wordRelationService.selectWordRelationList(wordRelation);
        return getDataTable(list);
    }

    /**
     * 导出单词归属关系列表
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:export')")
    @Log(title = "单词归属关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WordRelation wordRelation) {
        List<WordRelation> list = wordRelationService.selectWordRelationList(wordRelation);
        ExcelUtil<WordRelation> util = new ExcelUtil<WordRelation>(WordRelation.class);
        return util.exportExcel(list, "wordrelation");
    }

    /**
     * 获取单词归属关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(wordRelationService.selectWordRelationById(id));
    }

    /**
     * 新增单词归属关系
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:add')")
    @Log(title = "单词归属关系", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody WordRelation wordRelation) {

        if (wordRelation.getWordIds().length > 0) {
            // 获取数据
            List<WordRelation> list = this.wordRelationService.list(new LambdaQueryWrapper<WordRelation>()
                    .eq(WordRelation::getBelongId, wordRelation.getBelongId())
                    .in(WordRelation::getWordId, wordRelation.getWordIds())
            );
            Map<String, String> map = list.stream().collect(Collectors.toMap(WordRelation::getWordId, WordRelation::getWordId));

            List<WordRelation> wordRelationList = new ArrayList<>();
            for (String wordid : wordRelation.getWordIds()) {
                if (map.containsKey(wordid)) continue;

                WordRelation word = new WordRelation();
                word.setBelongId(wordRelation.getBelongId());
                word.setWordId(wordid);
                word.setCreateTime(DateUtils.getNowDate());
                word.setCreateBy(SecurityUtils.getUsername());
                wordRelationList.add(word);
            }
            if (CollectionUtil.isNotEmpty(wordRelationList)) {
                return toAjax(wordRelationService.insertWordRelationList(wordRelationList));
            } else {
                return AjaxResult.error("单词已添加");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改单词归属关系
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:edit')")
    @Log(title = "单词归属关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WordRelation wordRelation) {
        return toAjax(wordRelationService.updateWordRelation(wordRelation));
    }

    /**
     * 删除单词归属关系
     */
    @PreAuthorize("@ss.hasPermi('study:wordrelation:remove')")
    @Log(title = "单词归属关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wordRelationService.deleteWordRelationByIds(ids));
    }
}
